<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/9/24
 * Time: 20:06
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use app\admin\model\ReasonModel;
use app\admin\model\RecordModel;
use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use app\admin\model\ScoreModel;
use app\admin\model\StudentModel;
use think\Db;
use think\db\Query;

class RecordController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $data=Db::name('record')
            ->where(function (Query $query) use ($name) {
                if ($name) {
                    $query->where('ji_student.name', 'like', "%$name%");
                }
            })
            ->join('ji_student','student_id = ji_student.id','LEFT')
            ->join('ji_reason','reason_id = ji_reason.id')
            ->join('ji_score','score_id=ji_score.id')
            ->field('ji_record.*')
            ->field('ji_student.name as student_name,ji_reason.name as reason_name,ji_score.name as score_name')
            ->order('id desc')
            ->paginate(3, false, ['query' => request()->param()]);
        $data->appends(['name'=>$name]);
        $page=$data->render();
        return view('',['data'=>$data,'page'=>$page]);
    }

    public function add()
    {
        $school=SchoolModel::select();
        $reason =ReasonModel::select();
        $score =ScoreModel::select();
        return view('add',['school'=>$school,'reason'=>$reason,'score'=>$score]);
    }
    public function addPost()
    {
        $data = input();
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $add=RecordModel::create($data);
        if ($add){
            $this->success('添加成功','record/index');
        }else{
            $this->error('添加失败');
        }
    }

    public function getRoomBySchool()
    {
        $schoolId = input('school_id');
        $rooms = RoomModel::where('school_id',$schoolId)->select();
        $num = count($rooms);
        if ($num > 0) {
            $data=["<option value=''>请选择班级</option>"];
            foreach ($rooms as $v){
                $opt = "<option value='".$v->id."'>".$v->name."</option>";
                array_push($data,$opt);
            }
            return $data;
        }else{
            return"<option value=''>暂无数据</option>";
        }
    }

    public function getStudentByRoom()
    {
        $roomId = input('room_id');
        $student = StudentModel::where('room_id',$roomId)->select();
        if ($student){
            $data=["<option value=''>请选择学生</option>"];
            foreach ($student as $v){
                $opt = "<option value='".$v->id."'>".$v->name."</option>";
                array_push($data,$opt);
            }
            return $data;
        }else{
            return"<option value=''>暂无数据</option>";
        }
    }

    public function edit()
    {
        $id = input('id');
        $record = RecordModel::get($id);
        $school = SchoolModel::all();
        $reason = ReasonModel::all();
        $score = ScoreModel::all();
        $room_id = StudentModel::where('id', $record->student_id)->value('room_id');
        $school_id = RoomModel::where('id', $room_id)->value('school_id');
        $room = RoomModel::where('school_id', $school_id)->select();
        $student = StudentModel::where('room_id', $room_id)->select();
        return $this->fetch('', [
            'data' => $record,
            'school' => $school,
            'reason' => $reason,
            'score' => $score,
            'room_id' => $room_id,
            'school_id' => $school_id,
            'room' => $room,
            'student' => $student
        ]);
    }
    public function editPost()
    {
        $data = input();
        $edit=RecordModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','record/index');
        }else{
            $this->error('修改失败');
        }
    }


    public function delete()
    {
        $id=input('id');
        $del=RecordModel::destroy($id);
        if ($del){
            $this->success("删除成功！", url("record/index"));
        }else{
            $this->error("删除失败！");
        }
    }
}